//
//  TDInputYanZhengCodeC.m
//  TBRentClient
//
//  Created by tonbright on 2018/6/20.
//  Copyright © 2018年 tonbright. All rights reserved.
//

#import "TDInputYanZhengCodeC.h"
#import <JKCountDownButton.h>
#import "VertificationCodeInputView.h"
#import "NetWorkRequest.h"
#import "TDInputPasswordC.h"

@interface TDInputYanZhengCodeC ()<getTextFieldContentDelegate> {
    VertificationCodeInputView *vertificationCodeInputView;
}
@property (weak, nonatomic) IBOutlet UIView *view_inputYanZhengCode;
@property (weak, nonatomic) IBOutlet UILabel *lbe_placehold;
@property (weak, nonatomic) IBOutlet JKCountDownButton *btn_getYanZhengMa;
@property (weak, nonatomic) IBOutlet UILabel *lbe_phoneNum;

@end

@implementation TDInputYanZhengCodeC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];

}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
}

- (IBAction)closeBtnClick:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)setupUI {
    self.view.backgroundColor = [UIColor whiteColor];
    vertificationCodeInputView = [[VertificationCodeInputView alloc] initWithFrame:CGRectMake(25, 0, Width - 50, 45)];
    vertificationCodeInputView.delegate = self;
    /****** 设置验证码/密码的位数默认为四位 ******/
    vertificationCodeInputView.numberOfVertificationCode = 4;
    /*********验证码（显示数字）YES,隐藏形势 NO，数字形式**********/
    vertificationCodeInputView.secureTextEntry =NO;
    [self.view_inputYanZhengCode addSubview:vertificationCodeInputView];
    [vertificationCodeInputView.textField addTarget:self action:@selector(editingDidBegin:) forControlEvents:UIControlEventEditingDidBegin];
    [vertificationCodeInputView.textField addTarget:self action:@selector(editingDidEnd:) forControlEvents:UIControlEventEditingDidEnd];
    NSString *tmpPhoneStr = _phoneNumStr;
    tmpPhoneStr = [tmpPhoneStr stringByReplacingCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
    
    _lbe_phoneNum.text = tmpPhoneStr;
    [self getYanZhengMaBtnClick];
}

- (void)startCountDown {
    self.btn_getYanZhengMa.enabled = NO;
    [self.btn_getYanZhengMa startCountDownWithSecond:60.0];
    [self.btn_getYanZhengMa countDownChanging:^NSString *(JKCountDownButton *countDownButton, NSUInteger second) {
        NSString *title = [NSString stringWithFormat:@"剩余%ld秒",second];
        return title;
    }];
    [self.btn_getYanZhengMa countDownFinished:^NSString *(JKCountDownButton *countDownButton, NSUInteger second) {
        countDownButton.enabled = YES;
        return @"获取验证码";
    }];
}

#pragma mark --------- 获取验证码
-(void)returnTextFieldContent:(NSString *)content{
    NSLog(@"%@================验证码",content);
    [self.view endEditing:YES];
    [self checkYanZhengMaBtnClick:content];
}

- (void)editingDidBegin:(UITextField *)textfield {
    _lbe_placehold.hidden = YES;
}

- (void)editingDidEnd:(UITextField *)textfield {
    // MARK: - 如果是空的话
    if (!vertificationCodeInputView.label.text) {
        _lbe_placehold.hidden = NO;
    }else {
        _lbe_placehold.hidden = YES;
    }
}

- (IBAction)checkCodeBtnClick:(JKCountDownButton *)sender {
    [self.view endEditing:YES];
    //button type要 设置成custom 否则会闪动
    
    // MARK: - 获取验证码0-------
    [self getYanZhengMaBtnClick];
}

// MARK: - 获取验证码网络事件-------
- (void)getYanZhengMaBtnClick {
    
    NSDictionary *parDic = @{@"phone":_phoneNumStr};
    __weak typeof(self) weakSelf = self;
    [self progressHUDShowWithText:@"获取验证码..."];
    [NetWorkRequest postNetRequestData:getYanZhengMa parmeters:parDic response:^(NSDictionary *response) {
        if (response) {
            [weakSelf startCountDown];
            [weakSelf progressHUDShowWithCompleteText:@"验证码获取成功" isSucceed:YES];
        }else {
            [weakSelf progressHUDHideImmediately];
        }
    } netError:^(NSError *error) {
        [weakSelf progressHUDShowWithCompleteText:@"验证码获取失败" isSucceed:NO];
    }];
}


// MARK: - 验证验证码网络事件-------
- (void)checkYanZhengMaBtnClick:(NSString *)content {
    __weak typeof(self) weakSelf = self;
    VertificationCodeInputView *weakVertificationCodeInputView = vertificationCodeInputView;
    
    NSDictionary *parDic = @{@"phone":_phoneNumStr,@"verifycode":content};
    [self progressHUDShowWithText:@"获取验证码..."];
    [NetWorkRequest postNetRequestData:checkYanZhengMa parmeters:parDic response:^(NSDictionary *response) {
        [weakSelf progressHUDHideImmediately];
        if (response) {
            [AppSetting shareAppSetting].verifycode = content;
            TDInputPasswordC *inputPasswordC = [[TDInputPasswordC alloc] init];
            inputPasswordC.isRegister = weakSelf.isRegister;
            [weakSelf.navigationController pushViewController:inputPasswordC animated:YES];
            [weakVertificationCodeInputView resignFirstResponder];
            weakVertificationCodeInputView.vertificationCode = nil;
            weakVertificationCodeInputView.label.text = nil;
            weakVertificationCodeInputView.textField.text = nil;
        }else {
            [weakVertificationCodeInputView resignFirstResponder];
            weakVertificationCodeInputView.vertificationCode = nil;
            weakVertificationCodeInputView.label.text = nil;
            weakVertificationCodeInputView.textField.text = nil;
        }
    } netError:^(NSError *error) {
        weakSelf.noticeText = @"验证码验证失败";
        [weakVertificationCodeInputView resignFirstResponder];
        weakVertificationCodeInputView.vertificationCode = nil;
        weakVertificationCodeInputView.label.text = nil;
        weakVertificationCodeInputView.textField.text = nil;
    }];
}


@end
